package com.example.demo.controller;

import com.example.demo.entity.Cart;
import com.example.demo.exception.BusinessException;
import com.example.demo.service.CartService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;

/**
 * @author chenqiuran
 * @decription
 * @date 2021/9/3
 */
public class KafkaConsumer {

    protected Logger logger = LogManager.getLogger(BaseController.class);

    @Autowired
    private CartService cartService;

    @KafkaListener(topics = {"topic1"})
    public String consume(String message) throws BusinessException {
        String[] strings = message.split(",");
        logger.info("consume message:{}",message);
        if(strings[0].equals("CartAdd")){
            Cart cart = new Cart();
            cart.setUserId(new Long(strings[1]));
            cart.setMenuId(new Long(strings[2]));
            cart.setNumber(new Integer(strings[3]));
            cartService.addCart(cart);
        }
        if(strings[0].equals("updateCart")){
            Cart cart = new Cart();
            cart.setUserId(new Long(strings[1]));
            cart.setMenuId(new Long(strings[2]));
            cart.setNumber(new Integer(strings[3]));
            cartService.updateCart(cart);
        }
        if(strings[0].equals("delCart")){
            Long userId=new Long(strings[1]);
            Long menuId=new Long(strings[2]);
            cartService.delCart(userId,menuId);
        }

        return message;
    }
}
